package com.yjhb.android.sbz.activity;

import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.RechargeHistoryAdapter;
import com.yjhb.android.sbz.entity.Order;
import com.yjhb.android.sbz.mvp.rechargehistory.RechargeHistoryPresenter;
import com.yjhb.android.sbz.mvp.rechargehistory.RechargeHistoryView;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.jojo.android.zxlib.view.LoadMoreListView;
import com.jojo.android.zxlib.view.dialog.ILoading;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/3 16:40
 */
public class RechargeHistoryActivity extends BaseActivity<RechargeHistoryPresenter> implements RechargeHistoryView, OnRefreshListener, OnRefreshLoadmoreListener {
    private final String TAG = "RechargeHistoryActivity";

    @BindView(R.id.listView)
    LoadMoreListView listView;

    @BindView(R.id.loading_progress)
    ProgressBar loading_progress;
    @BindView(R.id.noMore_tips_tv)
    TextView noMore_tips_tv;
    @BindView(R.id.layout_empty)
    View layout_empty;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    ILoading loadingDialog;
    List<Order> data = new ArrayList<>();
    RechargeHistoryAdapter adapter;
    private final int size = 25;
    private int current = 1;

    @Override
    protected RechargeHistoryPresenter initPresenter() {
        return new RechargeHistoryPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge_history;
    }

    @Override
    protected void initUI() {
        listView.setEmptyView(layout_empty);
        listView.setDividerHeight(0);
        listView.setDivider(new ColorDrawable(0));
        listView.setEnabledLoadMore(false);
        listView.setAdapter(adapter = new RechargeHistoryAdapter(this));
        adapter.setData(data);

        loadingDialog = LoadingDialogTool.createDialog(this);
    }

    @Override
    protected void initData() {
        loadingDialog.show();
        presenter.getHistory(size, current);
    }

    @Override
    protected void regEvent() {
        refreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }


    @Override
    public void onWait() {

    }

    @Override
    public void onLoadHistory(List<Order> data) {
        refreshLayout.finishLoadmore();
        this.data.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onSuccess(List<Order> data) {
        loadingDialog.cancel();
        refreshLayout.finishRefresh();
        this.data.clear();
        this.data.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getHistory(size, current);
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        presenter.getHistory(size, ++current);
    }
}
